<!DOCTYPE html>
<html lang="en" >
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/BlogAdmin/layui/css/layui.css" media="all">
</head>
<body>
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend style="text-align: center">iArchie博客</legend>
</fieldset>
<div style="padding: 20px; background-color: #F2F2F2;">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">分类管理信息</div>

                    <div class="layui-card-body">
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">新的分类名:</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="categoryname" id="categoryname" placeholder="请输入新的分类名" lay-verify="required" autocomplete="off"
                                           class="layui-input"/>
                                </div>
                            </div>
                            <button id="addCategoryBtn" class="layui-btn layui-btn-warm layui-btn-radius">点击添加</button>
                        </div>
                        <table id="demo" lay-filter="test"></table>
                    </div>

            </div>
        </div>
    </div>
</div>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script src="/BlogAdmin/layui/layui.js"></script>
<script src="/LayuiBlog/js/jquery-min.js"></script>
<script>
    $("#addCategoryBtn").click(function () {
        // alert($("#categoryname").val());
        var categoryname=$("#categoryname").val();
        $.ajax({
            type: 'post',
            url: "/admin/addCategory",
            // contentType: "application/x-www-form-urlencoded",
            dataType: "json",
            data:{
                categoryname:categoryname
            },
            success: function (res) {
                if (res.result == 1) {
                    layer.msg("增加分类成功！", {icon: 6});
                    window.location.reload();
                } else {
                    layer.msg("增加分类失败！", {icon: 5});
                }
            },
            error: function () {
                layer.msg("增加分类失败！", {icon: 5});
            }
        })
    })
</script>
<script>
    layui.use('table', function () {
        var table = layui.table;

        //第一个实例
        table.render({
            elem: '#demo'
            , height: 600
            , url: '/admin/getCategoryList'
            , page: true
            ,parseData: function(res){ //res 即为原始返回的数据
                return {
                    "code": 0, //解析接口状态
                    "msg": "", //解析提示文本
                    "count": res.count, //解析数据长度
                    "data": res.data //解析数据列表
                };
            }
            , request: {
                pageName: 'pageNum', //页码的参数名称，默认：page
                limitName: 'pageSize' //每页数据量的参数名，默认：limit
            }
            , cols: [
                        [
                            {type: 'checkbox', fixed: 'left'}
                            , {field: 'categoryId', title: '分类ID', width: 100, sort: true}
                            , {field: 'categoryname', title: '分类名', width: 120}
                            , {field: 'id', title: '文章id', width: 160}
                            , {field: 'title', title: '文章标题', width: 200}
                            , {field: 'description', title: '文章描述', width: 200}
                            , {
                            fixed: 'right',
                            title: '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;操作',
                            toolbar: '#barDemo',
                            width: 180
                        }
                    ]
                ]
        });
        //监听行工具事件
        table.on('tool(test)', function (obj) {
            var data = obj.data;
            //console.log(obj)
            if (obj.event === 'del') {
                layer.confirm('真的删除ID为：'+data.categoryId+"的类别吗？", function (index) {
                    $.ajax({
                        type: 'post',
                        url: "/admin/deleteCategoryByCategoryId",
                        // contentType: "application/x-www-form-urlencoded",
                        dataType: "json",
                        data:{
                            categoryId:data.categoryId
                        },
                        success: function (res) {
                            if (res.result == 1) {
                                layer.msg("删除评论成功！", {icon: 6});
                            } else {
                                layer.msg("删除评论失败！", {icon: 5});
                            }
                        },
                        error: function () {
                            layer.msg("删除评论失败！", {icon: 5});
                        }
                    })
                    obj.del();
                    layer.close(index);
                });
            } else if (obj.event === 'edit') {
                layer.open({
                    type: 2,
                    area: ['45%', '400px'],
                    title: '编辑评论',
                    skin: 'layui-layer-rim', //加上边框
                    btn: ['保存', '取消'],
                    btnAlign: 'c',
                    top: 100,
                    content: ['/admin/layer_model4', 'no'],
                    // content:$("#popUpdateTest") //引用的弹出层的页面层的方式加载修改界面表单
                    success: function (layero,index) {
                        var body=layui.layer.getChildFrame('body',index);
                        body.find("[name=commentId]").val(data.commentId);
                        body.find("[name=parentCommentId]").val(data.parentCommentId);
                        body.find("[name=commentAvatar]").val(data.avatar);
                        body.find("[name=commentContent]").val(data.conmentcontent);
                    },
                    yes:function (index) {
                        var body=layui.layer.getChildFrame('body',index);
                        var commentId=body.find("[name=commentId]").val();
                        var parentCommentId=body.find("[name=parentCommentId]").val();
                        var commentAvatar=body.find("[name=commentAvatar]").val();
                        var commentContent=body.find("[name=commentContent]").val();
                        var blogId=data.blogId;
                        var location=data.location;
                        var nickname=data.nickname;
                        $.ajax( {
                            url:"/admin/updateCommentByCommentId",
                            dataType:"json",
                            type:"POST",
                            data:{
                                commentId:commentId,
                                parentCommentId:parentCommentId,
                                commentAvatar:commentAvatar,
                                commentContent:commentContent,
                                blogId:blogId,
                                location:location,
                                nickname:nickname
                            },
                            success: function (res) {
                                if (res.result == 1) {
                                    obj.update({
                                        commentId:commentId,
                                        parentCommentId:parentCommentId,
                                        commentAvatar:commentAvatar,
                                        commentContent:commentContent,
                                        blogId:blogId,
                                        location:location,
                                        nickname:nickname
                                    });
                                    layer.msg("更新博客文章成功！", {icon: 6});
                                    layer.close(index);
                                } else {
                                    layer.msg("更新博客文章失败！", {icon: 5});
                                }
                            }
                        })
                    }
                });
            }

        });
    });


</script>
</body>
</html>